﻿using NAnt.Core.Attributes;

namespace NAnt.SVN.Tasks
{
    /// <summary>
    /// Implementation of the checkout (co) command.
    /// You can run this task with the svncheckout command in NAnt
    /// $Id$
    /// </summary>
    [TaskName("svncheckout")]
    public class Checkout : SVNTask
    {
        #region Private Fields

        string url = string.Empty;
        bool nonrecursive = false;
        string user = string.Empty;
        string password = string.Empty;
        string revision = string.Empty;
        bool noauthcache = false;

        #endregion

        #region Public Fields

        [TaskAttribute("url", Required = true)]
        [StringValidator()]
        public string Url
        {
            get 
            {
                return url;
            }
            set
            {
                url = value;
            }
        }

        [TaskAttribute("revision", Required = false)]
        [StringValidator()]
        public string Revision
        {
            get
            {
                return revision;
            }
            set
            {
                revision = value;
            }
        }

        [TaskAttribute("user", Required = false)]
        [StringValidator()]
        public string User
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }

        [TaskAttribute("password", Required = false)]
        [StringValidator()]
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        [TaskAttribute("nonrecursive", Required = false)]
        [BooleanValidator()]
        public bool NonRecursive
        {
            get
            {
                return nonrecursive;
            }
            set
            {
                nonrecursive = value;
            }
        }

        [TaskAttribute("noauthcache", Required = false)]
        [BooleanValidator()]
        public bool NoAuthCache
        {
            get
            {
                return noauthcache;
            }
            set
            {
                noauthcache = value;
            }
        }

        /// <summary>
        /// Check if the checkout has to be performed with 
        /// authentication or not.
        /// </summary>
        public bool HasCredentials
        {
            get
            {
                return !string.IsNullOrEmpty(User) &&
                    !string.IsNullOrEmpty(Password);
            }
        }

        /// <summary>
        /// True if it has to be checked out to a specific revision
        /// </summary>
        public bool HeadRevision
        {
            get
            {
                return string.IsNullOrEmpty(Revision);
            }
        }

        #endregion

        /// <summary>
        /// Initialize command
        /// </summary>
        protected override void InitializeSVNTask()
        {
            Command = SVNCommands.CHECKOUT;
            Options.Add(Url);
            if (!HeadRevision)
            {
                Options.Add(SVNFlags.REVISION);
                Options.Add(Revision);
            }
            if (HasCredentials)
            {
                Options.Add(SVNFlags.USER);
                Options.Add(quote(User));
                Options.Add(SVNFlags.PASSWORD);
                Options.Add(quote(Password));
                if (NoAuthCache) Options.Add(SVNFlags.NOAUTHCACHE);
            }
            if (NonRecursive)
                Options.Add(SVNFlags.NONRECURSIVE);
        }
    }
}
